home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / programming / arexx / splitosdocs / splitosdocs.rexx < prev    next >
OS/2 REXX Batch file  |  1999-11-30  |  7KB  |  210 lines

  1. /*
  2.    SplitOSDocs.rexx Macro
  3.    Splits OS3.5 HTML manuals into manageable-sized chapters
  4.    $VER: SplitOSDocs.rexx v1.00 (1 Nov 1999)
  5.    ©Ron Goertz (goertz@earthlink.net)
  6. */
  7.  
  8. options results
  9.  
  10. BS           = '08'x
  11. LF           = '0a'x
  12. ChunkSize    = 10000
  13. ManualName.0 = 'arexx'
  14. ManualName.1 = 'dos'
  15. ManualName.2 = 'harddisk'
  16. ManualName.3 = 'installation'
  17. ManualName.4 = 'workbench'
  18. DocName.0    = 'book-main.html'
  19. DocName.1    = 'book-index.html'
  20. DocName.2    = 'book-toc.html'
  21. DocName.3    = 'index.html'
  22. Header1      = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML EXPERIMENTAL 970324//EN">'LF'<HTML>'LF'<TITLE>'
  23. Header2      = '</TITLE>'LF'</HEAD>'LF'<BODY BGCOLOR="#ffffff">'
  24. Footer       = '</BODY>'LF'</HTML>'
  25. SourcePath   = ''
  26. DestPath     = ''
  27.  
  28. call addlib('rexxreqtools.library', 0, -30, 0)
  29. call addlib('rexxsupport.library', 0, -30, 0)
  30.  
  31. address command 'REQUESTFILE >t:Path DRAWER "AmigaOS3.5:OS-Version3.5/AmigaOS 3.5 Manual" TITLE "Select the original document directory:" DRAWERSONLY NOICONS'
  32. call open('Temp', 't:Path');SourcePath = compress(readln('Temp'), '"');call close('Temp')
  33.  
  34. address command 'REQUESTFILE >t:Path TITLE "Select destination directory:" DRAWERSONLY NOICONS'
  35. call open('Temp', 't:Path');DestPath = compress(readln('Temp'), '"');call close('Temp')
  36. address command 'delete t:Path quiet'
  37. if (rc == 5) | (SourcePath == '') | (DestPath == '') then exit
  38.  
  39. call UpdateProgress('Copying root files and images...')
  40.   address command 'copy "'SourcePath'#?.html" "'DestPath'" quiet'
  41.   address command 'copy "'SourcePath'graphics" "'DestPath'graphics" all quiet'
  42.  
  43. do Manual = 0 to 4
  44.   call time(r)
  45.   CurrentNode = ''
  46.   FirstNode   = ''
  47.   Manual$ = 'Converting 'ManualName.Manual' manual...'LF
  48.   SrcPath = SourcePath''ManualName.Manual'/'
  49.   DstPath = DestPath''ManualName.Manual'/'
  50.   call makedir(left(DstPath, length(DstPath) - 1))
  51.  
  52.   call UpdateProgress(Manual$'  Copying root files and images...')
  53.   address command 'copy "'SrcPath'#?.gif" "'DstPath'" quiet'
  54.   address command 'copy "'SrcPath'menu.html" "'DstPath'" quiet'
  55.   address command 'copy "'SrcPath'head.html" "'DstPath'" quiet'
  56.  
  57.   Category$ = Manual$'  Standardizing document formats...'LF
  58.  
  59.   do i = 0 to 3
  60.     call Standardize(DocName.i)
  61.   end
  62.  
  63.   Category$ = Manual$'  Splitting 'DocName.0'...'LF
  64.   call open('Src', DstPath'-'DocName.0)
  65.   do until EOF('Src')
  66.     Ln = ReadLn('Src')
  67.     if Ln == '<DIV>' then do
  68.       ReturnPos = seek('Src', 0)
  69.       NextLn = ReadLn('Src')
  70.       call seek('Src', ReturnPos, 'B')
  71.       if (Manual == 2) then do
  72.         if pos('<H2 CLASS="Überschrift1">', NextLn) > 0 then NextLn = '<H1 CLASS="Titel">'
  73.       end
  74.     end
  75.     else NextLn = ''
  76.     if eof('Src') then leave
  77.     select
  78.       when Ln == '</BODY>' then do
  79.         call writeln('Dst', Footer)
  80.         call close('Dst')
  81.         leave
  82.       end
  83.       when (pos('<H1 CLASS="Titel">', Ln) > 0) & (pos('</H', Ln) > 0) then do
  84.         StartPos = pos('</A>', Ln)
  85.         EndPos = pos('</H', Ln)
  86.         NodeName = strip(substr(Ln, StartPos + 4, EndPos - StartPos - 4))
  87.         call SetNode(NodeName)
  88.         if pos('<A NAME=', Ln) > 0 then do
  89.           KeyStart = pos('=', Ln, pos('<A NAME=', Ln) + 8) + 1
  90.           KeyEnd = pos('"', Ln, KeyStart)
  91.           Key = substr(Ln, KeyStart, KeyEnd - KeyStart)
  92.           Name.Key = CurrentNode
  93.         end
  94.       end
  95.       when (Ln == '<DIV>') & ((pos('<H1 CLASS="Titel">', NextLn) > 0) & (pos('</H', NextLn) == 0)) then do
  96.         ReturnPos = seek('Src', 0)
  97.         do forever
  98.           TLn = readln('Src')
  99.           if pos('</H', TLn) > 0 then leave
  100.         end
  101.         call seek('Src', ReturnPos, 'B')
  102.         NodeName = left(TLn, pos('</H', TLn) - 1)
  103.         call SetNode(NodeName)
  104.       end
  105.       when pos('<A NAME=', Ln) > 0 then do
  106.         KeyStart = pos('=', Ln, pos('<A NAME=', Ln) + 8) + 1
  107.         KeyEnd = pos('"', Ln, KeyStart)
  108.         Key = substr(Ln, KeyStart, KeyEnd - KeyStart)
  109.         Name.Key = CurrentNode
  110.       end
  111.       otherwise nop
  112.     end
  113.     if CurrentNode ~= '' then call writeln('Dst', Ln)
  114.   end
  115.   call Close('Src')
  116.   address command 'delete "'DstPath'-'DocName.0'" quiet'
  117.  
  118.   do i = 1 to 2
  119.     call UpdateProgress(Manual$'  Updating 'DocName.i'...')
  120.     call open('Src', DstPath'-'DocName.i)
  121.     call open('Dst', DstPath''DocName.i, 'W')
  122.     do until EOF('Src')
  123.       Ln = ReadLn('Src')
  124.       if eof('Src') then leave
  125.       if pos('<A HREF="book-main.html#', Ln) > 0 then do
  126.         Key = substr(Ln, pos('=', Ln, 25) + 1)
  127.         Key = left(Key, pos('"', Key) - 1)
  128.         Ln = ReplaceString('book-main.html', Name.Key, Ln)
  129.       end
  130.       call writeln('Dst', Ln)
  131.     end
  132.     call close('Dst')
  133.     call close('Src')
  134.     address command 'delete "'DstPath'-'DocName.i'" quiet'
  135.   end
  136.  
  137.   call UpdateProgress(Manual$'  Updating 'DocName.3'...')
  138.   call open('Src', DstPath'-'DocName.3)
  139.   call open('Dst', DstPath''DocName.3, 'W')
  140.     call writech('Dst', ReplaceString('book-main.html', FirstNode, readch('Src', 65535)))
  141.   call close('Dst')
  142.   call close('Src')
  143.   address command 'delete "'DstPath'-'DocName.3'" quiet'
  144.  
  145.   Time.Manual = time(e)
  146. end
  147. writeln(stdout, '0c'x||'Done!')
  148. do i = 0 to 4
  149.   writeln(stdout, 'It took 'trunc(Time.i / 60, 2)' minutes to convert the 'ManualName.i' manual.')
  150. end
  151. exit
  152.  
  153. /***//*******  ReplaceString Subroutine ***********/
  154. ReplaceString: PROCEDURE
  155.   parse arg old, new, string
  156.  
  157.   StartPos = pos(old, string)
  158.   do while StartPos > 0
  159.     parse var string begin(old)end
  160.     string = begin''new''end
  161.     StartPos = pos(old, string)
  162.   end
  163.   return string
  164. /**/
  165.  
  166. /***//*******  SetNode Subroutine  ***********/
  167. SetNode:
  168.   if CurrentNode ~= '' then do
  169.     call writeln('Dst', Footer)
  170.     call close('Dst')
  171.   end
  172.   call UpdateProgress(Category$'    'NodeName)
  173.   CurrentNode = translate(NodeName, xrange('a','z'), xrange('A', 'Z'))
  174.   if words(CurrentNode) > 1 then do
  175.     CurrentNode = substr(CurrentNode, max(1, max(pos('chapter', CurrentNode), pos('appendix', CurrentNode))))
  176.     CurrentNode = compress(subword(CurrentNode, 1, 2))
  177.   end
  178.   CurrentNode = CurrentNode'.html'
  179.   if FirstNode == '' then FirstNode = CurrentNode
  180.   call open('Dst', DstPath''CurrentNode, 'W')
  181.   call writeln('Dst', Header1''NodeName''Header2)
  182.   return
  183. /**/
  184.  
  185. /***//*******  Standardize Subroutine ***********/
  186. Standardize:
  187.   parse arg DocName
  188.  
  189.   call UpdateProgress(Category$'    'DocName)
  190.   call writech(stdout, copies('.', trunc(word(statef(SrcPath''DocName), 2)/ChunkSize + 2)))
  191.   call Open('Src', SrcPath''DocName)
  192.   call Open('Dst', DstPath'-'DocName, 'W')
  193.     do until eof('Src')
  194.       call writech(stdout, BS' 'BS)
  195.       call writech('Dst', translate(ReplaceString('0d'x||'0a'x, '0a'x, compress(readch('Src', ChunkSize), '09'x)), '0a'x, '0d'x))
  196.     end
  197.   call close('Dst')
  198.   call close('Src')
  199.  
  200.   return 0
  201. /**/
  202.  
  203. /***//*******  UpdateProgress Subroutine ***********/
  204. UpdateProgress:
  205.   parse arg Msg
  206.   writech(stdout, '0c'x||Msg)
  207.   return
  208. /**/
  209.  
  210.